//
//  LGDeviceManagerTabController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/25.
//

#import "LGDeviceManagerTabController.h"

#import "LGDeviceManageTableController.h"


#import "LGRecommendDoctorController.h"
@interface LGDeviceManagerTabController ()<UITextFieldDelegate>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**  全部 */
@property (nonatomic , strong)LGDeviceManageTableController *totalVC;
/**  设备发货 */
@property (nonatomic , strong)LGDeviceManageTableController *deliverVC;
/**  设备回收 */
@property (nonatomic , strong)LGDeviceManageTableController *recycleVC;
/**   */
@property (nonatomic , strong)NSMutableArray *controllerArray;
/**   */
@property (nonatomic , copy)NSString *deliverTitle;
/**   */
@property (nonatomic , copy)NSString *recycleTitle;
/**   */
@property (nonatomic , strong)NSArray *titleArray;

@end

@implementation LGDeviceManagerTabController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self commonInit];
    [self requestNumberData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNumberData) name:@"DeviceRefreshService" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestNumberData) name:@"DeviceRefreshShope" object:nil];
}

-(void)requestNumberData{
    if ([self.type isEqualToString:@"0"]) {
        [RequestUtil withPOST:@"/doctor/equipment.equipment/vipList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                self.deliverTitle = [NSString stringWithFormat:@"设备发货(%@)",returnData[@"shipwait_total"]];
                self.recycleTitle = [NSString stringWithFormat:@"设备回收(%@)",returnData[@"backwait_total"]];
                [self.menuView updateTitle:self.deliverTitle atIndex:1 andWidth:NO];
                [self.menuView updateTitle:self.recycleTitle atIndex:2 andWidth:NO];
            }
        } error:^(NSError *error) {
            
        }];
    }else{
        [RequestUtil withPOST:@"/doctor/equipment.equipment/mallList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                self.deliverTitle = [NSString stringWithFormat:@"设备发货(%@)",returnData[@"shipwait_total"]];
                self.recycleTitle = [NSString stringWithFormat:@"设备回收(%@)",returnData[@"backwait_total"]];
                [self.menuView updateTitle:self.deliverTitle atIndex:1 andWidth:NO];
                [self.menuView updateTitle:self.recycleTitle atIndex:2 andWidth:NO];
            }
        } error:^(NSError *error) {
            
        }];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [self setSelectIndex:0];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *lang = textField.textInputMode.primaryLanguage;// 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textField markedTextRange];
        if (!selectedRange) { // 没有高亮
            NSString *content = [NSString stringWithFormat:@"%@",textField.text];
            [self.totalVC requestDataWithKeyword:content];
        }
    }else{
        NSString *content = [NSString stringWithFormat:@"%@",textField.text];
        [self.totalVC requestDataWithKeyword:content];
    }
}

#pragma mark
#pragma mark ====> WMPageControllerDelegate

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.controllerArray.count;
}


- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    if (index == 0) {
        return @"全部";
    }else if (index == 1){
        return self.deliverTitle;
    }else{
        return self.recycleTitle;
    }
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    return self.controllerArray[index];
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    if (index == 0) {
        return viewPix(90);
    }
    return (Screen_W-viewPix(100))/2.0;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(0, viewPix(45), Screen_W-viewPix(10), viewPix(44));
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    return CGRectMake(0, viewPix(44)+viewPix(45), Screen_W, Screen_H-topBarHeight-viewPix(44)-viewPix(45));
}

- (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    if ([info[@"index"] integerValue] != 0 && self.searchTF.isFirstResponder == YES) {
        [self.searchTF resignFirstResponder];
    }
    
    LGBaseViewController *controller = (LGBaseViewController *)viewController;
//    [controller requestData];
    NSLog(@">>>>>willEnterView>>>%@",viewController.title);
}

- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    NSLog(@">>>>>didEnterView>>>%@",viewController.title);
}


#pragma mark
#pragma mark ====> 创建控件
-(void)commonInit{
    self.deliverTitle = @"设备发货";
    self.recycleTitle = @"设备回收";
    [self.view addSubview:self.searchTF];
    [self addChildViewController:self.totalVC];
    [self addChildViewController:self.deliverVC];
    [self addChildViewController:self.recycleVC];
    [self.controllerArray addObject:self.totalVC];
    [self.controllerArray addObject:self.deliverVC];
    [self.controllerArray addObject:self.recycleVC];
    
    self.titleColorNormal = [UIColor colorWithString:@"#909399"];
    self.titleColorSelected = [UIColor colorWithString:@"#282828"];
    self.progressColor = [UIColor colorWithString:@"#3291FF"];
    self.menuViewStyle=WMMenuViewStyleLine;
    self.titleFontName = @"STHeitiTC-Medium";
    self.titleSizeSelected = 18;
    self.titleSizeNormal = 16;
    self.progressWidth = viewPix(20);
    self.progressHeight = 4;
    self.progressViewBottomSpace = 6;
    self.progressViewCornerRadius = 2;
    self.menuView.backgroundColor = [UIColor clearColor];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];
    [self reloadData];
    
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(10), Screen_W-viewPix(32), viewPix(34))];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"输入患者姓名/手机号/设备编号搜索";
        _searchTF.cornerRidus = viewPix(16);
        _searchTF.font = LGFont(17);
        _searchTF.delegate = self;
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
    }
    return _searchTF;
}

-(UIView *)leftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(35), viewPix(34))];
    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(9), viewPix(16), viewPix(16))];
    iconView.image = [UIImage imageNamed:@"searchGray16"];
    [baseView addSubview:iconView];
    return baseView;
}


-(NSMutableArray *)controllerArray{
    if(!_controllerArray){
        _controllerArray = [NSMutableArray array];
    }
    return _controllerArray;
}

-(LGDeviceManageTableController *)totalVC{
    if(!_totalVC){
        _totalVC = [[LGDeviceManageTableController alloc] init];
        _totalVC.type = self.type;
        if ([self.type isEqualToString:@"0"]) {
            //服务包
            _totalVC.action = @"/doctor/equipment.equipment/vipList";
            _totalVC.key_name = @"state";
            _totalVC.key_value = @"";
            _totalVC.notifi_name = @"DeviceRefreshService";
            
        }else{
            //商城
            _totalVC.action = @"/doctor/equipment.equipment/mallList";
            _totalVC.key_name = @"ship_state";
            _totalVC.key_value = @"";
            _totalVC.notifi_name = @"DeviceRefreshShope";
        }
    }
    return _totalVC;
}

-(LGDeviceManageTableController *)deliverVC{
    if(!_deliverVC){
        _deliverVC = [[LGDeviceManageTableController alloc] init];
        _deliverVC.type = self.type;
        if ([self.type isEqualToString:@"0"]) {
            //服务包
            _deliverVC.action = @"/doctor/equipment.equipment/vipList";
            _deliverVC.key_name = @"state";
            _deliverVC.key_value = @"20";
            _deliverVC.notifi_name = @"DeviceRefreshService";
            
        }else{
            //商城
            _deliverVC.action = @"/doctor/equipment.equipment/mallList";
            _deliverVC.key_name = @"ship_state";
            _deliverVC.key_value = @"0";
            _deliverVC.notifi_name = @"DeviceRefreshShope";
        }
    }
    return _deliverVC;
}


-(LGDeviceManageTableController *)recycleVC{
    if(!_recycleVC){
        _recycleVC = [[LGDeviceManageTableController alloc] init];
        _recycleVC.type = self.type;
        if ([self.type isEqualToString:@"0"]) {
            //服务包
            _recycleVC.action = @"/doctor/equipment.equipment/vipList";
            _recycleVC.key_name = @"state";
            _recycleVC.key_value = @"103";
            _recycleVC.notifi_name = @"DeviceRefreshService";
            
        }else{
            //商城
            _recycleVC.action = @"/doctor/equipment.equipment/mallList";
            _recycleVC.key_name = @"ship_state";
            _recycleVC.key_value = @"3";
            _recycleVC.notifi_name = @"DeviceRefreshShope";
        }
    }
    return _recycleVC;
}



@end
